﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.Win32;
using Microsoft.MediaCenter.UI;
using ClipShow2.LibraryManager;
using ClipShow2.MediaTypes;

namespace ClipShowShell.Code
{
    public class Start : ModelItem
    {
        private const string THUMB_EXE_REG_KEY = @"Software\xPlug\clipShow";
        private Notifier _mediaAvailable;
        private Command _launchBrowserShell;
        private ClipShowContent _mediaLibrary;
        private string _moreVideoContextUrl = "";
        MarkupLoader _addInHostApplication;
        public Start()
        {
            _launchBrowserShell = new Command();
            FindMedia();
        }

        public void FindMedia()
        {
            // Wire up appropriate event handler for new media notifier
            _mediaAvailable = new Notifier();
            _mediaAvailable.NotifierAlert += new NotifierEventHandler(MediaAvailable_NotifierAlert);
            _mediaAvailable.NotifierException += new NotifierExceptionHandler(MediaAvailable_NotifierException);
            _mediaAvailable.Start();
        }

        void MediaAvailable_NotifierException(object sender, NotifierExceptionArgs e)
        {
            AddInHost.Current.MediaCenterEnvironment.Dialog(e.NotifierExceptionText, "Media Search", DialogButtons.Ok, 30000, true);
            AddInHost.Current.ApplicationContext.CloseApplication();
        }
        void MediaAvailable_NotifierAlert(object sender, NotifierEventArgs e)
        {
            Application.DeferredInvoke(InitializeLists, e);
        }

        internal void InitializeLists(object Val)
        {
            NotifierEventArgs e = (NotifierEventArgs)Val;
            _mediaLibrary = e.NotifierLibrary;

            try
            {
                Process x = new Process();
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(THUMB_EXE_REG_KEY);
                if (rk == null)
                {
                    x.StartInfo.FileName = @"C:\Sandbox\clipShow 2.0\Thumbs\Thumbs\bin\Debug\Thumbs.exe";
                }
                else
                {
                    string filePath = (string)rk.GetValue("ThumbExePath");
                    x.StartInfo.FileName = filePath;
                }
                x.Start();
            }
            catch (SystemException se)
            {
                AddInHost.Current.MediaCenterEnvironment.Dialog(
                    se.Message, "Startup", DialogButtons.Ok, 30000, true);
                AddInHost.Current.ApplicationContext.CloseApplication();
            }

            // Notify start screen that we are ready to load the browser shell UI
            _launchBrowserShell.Invoke();
        }

        public BrowserShell BrowseCodeBehind
        {
            get
            {
                BrowserShell mb = new BrowserShell();
                mb.InitializeLists(_mediaLibrary, _moreVideoContextUrl);
                return (mb);
            }
        }
        public Command LaunchBrowserShell
        {
            get { return (_launchBrowserShell); }
        }
        public ClipShowContent MediaLibrary
        {
            get { return (_mediaLibrary); }
        }
        public MarkupLoader MyAddInHost
        {
            set { _addInHostApplication = value; }
        }

    }
}
